'use strict';

/**
 * @ngdoc directive
 * @name webApp.directive:communicate
 * @description
 * # communicate
 */

angular.module('webApp')
  .directive('communicate', function ($rootScope,cookie,promiseCommunicate,popBox,CacheFactory,$location,$localStorage) {
  return {
    templateUrl: 'views/communicateBox.html',
    restrict: 'AE',
    replace: false,
    scope: {
      communicateData: '=communicateData'
    },
    compile: function(tele,tattr){
      return{
        pre: function($scope,tele,tattr){

        },
        post: function($scope,iele,iattr){
          //事件传递
          $scope.commentBacks = function(id,flag){
            $rootScope.$broadcast('footShow',{title: '写回复',action: 'show'});
            cookie.setCookie('replayId',id);
            $localStorage.flag = flag;
          };
          //回复隐藏
          if($location.$$path != '/root/userCommunicate'){
            $scope.priseHands = true;
          }
          else{
            $scope.priseHands = false;
          }
          //点赞
          $scope.favours = function(id,boole,flag){
            if(cookie.getCookie('access_token')) {
              CacheFactory.clearAll();
              promiseCommunicate.favours(id, boole,flag).then(function (data) {
                for (var i = 0; i < $scope.communicateData.length; i++) {
                  if ($scope.communicateData[i].id == data.article_comment_id) {
                    $scope.communicateData[i].favours = data.favours;
                    $scope.communicateData[i].is_favoured = !$scope.communicateData[i].is_favoured;
                  }
                }
              });
            }
            else{
              popBox.showConfirm('警告','请先登录！');
            }
          };
        }
      }
    }
  };
});
